home *** CD-ROM | disk | FTP | other *** search
Wrap
Public Class PerformanceCounterForm Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents txtOut As System.Windows.Forms.TextBox Friend WithEvents cboCategories As System.Windows.Forms.ComboBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents cboCounters As System.Windows.Forms.ComboBox Friend WithEvents cboInstances As System.Windows.Forms.ComboBox Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents btnSysinfo As System.Windows.Forms.Button Friend WithEvents btnGetCategories As System.Windows.Forms.Button Friend WithEvents btnSamples As System.Windows.Forms.Button Friend WithEvents btnCreate As System.Windows.Forms.Button Friend WithEvents btnDestroy As System.Windows.Forms.Button Friend WithEvents btnDisplay As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Label3 = New System.Windows.Forms.Label() Me.txtOut = New System.Windows.Forms.TextBox() Me.btnSamples = New System.Windows.Forms.Button() Me.btnGetCategories = New System.Windows.Forms.Button() Me.btnSysinfo = New System.Windows.Forms.Button() Me.cboCounters = New System.Windows.Forms.ComboBox() Me.cboCategories = New System.Windows.Forms.ComboBox() Me.btnCreate = New System.Windows.Forms.Button() Me.btnDestroy = New System.Windows.Forms.Button() Me.cboInstances = New System.Windows.Forms.ComboBox() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.btnDisplay = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'Label3 ' Me.Label3.Location = New System.Drawing.Point(160, 120) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(184, 16) Me.Label3.TabIndex = 3 Me.Label3.Text = "Instances" ' 'txtOut ' Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.txtOut.Location = New System.Drawing.Point(160, 184) Me.txtOut.Multiline = True Me.txtOut.Name = "txtOut" Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both Me.txtOut.Size = New System.Drawing.Size(360, 136) Me.txtOut.TabIndex = 1 Me.txtOut.Text = "" ' 'btnSamples ' Me.btnSamples.Location = New System.Drawing.Point(16, 64) Me.btnSamples.Name = "btnSamples" Me.btnSamples.Size = New System.Drawing.Size(120, 40) Me.btnSamples.TabIndex = 0 Me.btnSamples.Text = "Get some samples" ' 'btnGetCategories ' Me.btnGetCategories.Location = New System.Drawing.Point(16, 112) Me.btnGetCategories.Name = "btnGetCategories" Me.btnGetCategories.Size = New System.Drawing.Size(120, 40) Me.btnGetCategories.TabIndex = 0 Me.btnGetCategories.Text = "Get Counter Categories" ' 'btnSysinfo ' Me.btnSysinfo.Location = New System.Drawing.Point(16, 16) Me.btnSysinfo.Name = "btnSysinfo" Me.btnSysinfo.Size = New System.Drawing.Size(120, 40) Me.btnSysinfo.TabIndex = 0 Me.btnSysinfo.Text = "Get some system info" ' 'cboCounters ' Me.cboCounters.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.cboCounters.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList Me.cboCounters.DropDownWidth = 360 Me.cboCounters.Location = New System.Drawing.Point(160, 88) Me.cboCounters.Name = "cboCounters" Me.cboCounters.Size = New System.Drawing.Size(360, 26) Me.cboCounters.Sorted = True Me.cboCounters.TabIndex = 2 ' 'cboCategories ' Me.cboCategories.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.cboCategories.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList Me.cboCategories.DropDownWidth = 360 Me.cboCategories.Location = New System.Drawing.Point(160, 32) Me.cboCategories.Name = "cboCategories" Me.cboCategories.Size = New System.Drawing.Size(360, 26) Me.cboCategories.Sorted = True Me.cboCategories.TabIndex = 2 ' 'btnCreate ' Me.btnCreate.Location = New System.Drawing.Point(16, 160) Me.btnCreate.Name = "btnCreate" Me.btnCreate.Size = New System.Drawing.Size(120, 40) Me.btnCreate.TabIndex = 0 Me.btnCreate.Text = "Create custom counters" ' 'btnDestroy ' Me.btnDestroy.Location = New System.Drawing.Point(16, 256) Me.btnDestroy.Name = "btnDestroy" Me.btnDestroy.Size = New System.Drawing.Size(120, 40) Me.btnDestroy.TabIndex = 0 Me.btnDestroy.Text = "Destroy custom conters" ' 'cboInstances ' Me.cboInstances.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.cboInstances.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList Me.cboInstances.DropDownWidth = 360 Me.cboInstances.Location = New System.Drawing.Point(160, 144) Me.cboInstances.Name = "cboInstances" Me.cboInstances.Size = New System.Drawing.Size(360, 26) Me.cboInstances.Sorted = True Me.cboInstances.TabIndex = 2 ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(160, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(184, 16) Me.Label1.TabIndex = 3 Me.Label1.Text = "Categories" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(160, 64) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(184, 16) Me.Label2.TabIndex = 3 Me.Label2.Text = "Counters" ' 'btnDisplay ' Me.btnDisplay.Location = New System.Drawing.Point(16, 208) Me.btnDisplay.Name = "btnDisplay" Me.btnDisplay.Size = New System.Drawing.Size(120, 40) Me.btnDisplay.TabIndex = 0 Me.btnDisplay.Text = "Display custom counters" ' 'PerformanceCounterForm ' Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17) Me.ClientSize = New System.Drawing.Size(536, 333) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnDisplay, Me.btnDestroy, Me.btnCreate, Me.btnSamples, Me.cboInstances, Me.Label3, Me.Label2, Me.cboCounters, Me.Label1, Me.cboCategories, Me.btnGetCategories, Me.txtOut, Me.btnSysinfo}) Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "PerformanceCounterForm" Me.Text = "EventLogForm" Me.ResumeLayout(False) End Sub #End Region ' demonstrates how you can read system info through performance counters Private Sub btnSysinfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSysinfo.Click ' Create a few performance counters. Dim pcProcesses As New PerformanceCounter("System", "Processes") Dim pcThreads As New PerformanceCounter("System", "Threads") Dim pcFilesOpen As New PerformanceCounter("Server", "Files Opened Total") ' Display their values. txtOut.Text = "" LogMessage("Processes = " & pcProcesses.RawValue.ToString) LogMessage("Threads = " & pcThreads.RawValue.ToString) LogMessage("Files Open Total = " & pcFilesOpen.RawValue.ToString) ' Create specific instances. Dim pcHandles As New PerformanceCounter("Process", "Handle Count", "devenv") LogMessage("Devenv Handle Count = " & pcHandles.RawValue.ToString) ' make it point to another instance. pcHandles.CounterName = "Thread Count" pcHandles.InstanceName = "System" LogMessage("System Thread Count = " & pcHandles.RawValue.ToString) Dim pcDevenvCpuTime As New PerformanceCounter("Process", "% Processor Time", "devenv") LogMessage("Devenv % Processor Time = " & pcDevenvCpuTime.NextValue.ToString) End Sub Sub LogMessage(ByVal msg As String) txtOut.AppendText(msg & ControlChars.CrLf) End Sub ' demonstrates how you can sample a counter Private Sub btnSamples_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSamples.Click Dim pcSwitches As New PerformanceCounter("Thread", "Context Switches/sec", "_Total") ' Read the first sample. Dim cs1 As CounterSample = pcSwitches.NextSample ' Wait for some time. System.Threading.Thread.Sleep(1000) ' Read the second sample. Dim cs2 As CounterSample = pcSwitches.NextSample ' Evaluate the result. Dim result As Single = CounterSample.Calculate(cs1, cs2) LogMessage("Thread switches/sec = " & result.ToString) Dim i As Integer For i = 1 To 10 System.Threading.Thread.Sleep(1000) ' Get a new sample after one second. Dim cs3 As CounterSample = pcSwitches.NextSample ' Evaluate "instantaneous" (moving) average. result = CounterSample.Calculate(cs2, cs3) LogMessage("Thread switches/sec = " & result.ToString) ' Evaluate average value since the first sample. result = CounterSample.Calculate(cs1, cs3) LogMessage("Average Thread switches/sec = " & result.ToString) ' Use most recent sample as the basis for next moving average. cs2 = cs3 Next End Sub ' get all the counter categories Private Sub btnGetCategories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetCategories.Click Dim pcc As PerformanceCounterCategory cboCategories.Items.Clear() ' Store categories in the combobox, but display their name. cboCategories.DisplayMember = "CategoryName" ' Load the combobox with all category names. For Each pcc In PerformanceCounterCategory.GetCategories cboCategories.Items.Add(pcc) Next ' Select the first element. cboCategories.SelectedIndex = 0 End Sub ' Fill the other two combobox controls when a category is selected. Private Sub cboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategories.SelectedIndexChanged Dim pcCat As PerformanceCounterCategory Dim pCnt As PerformanceCounter Dim instName As String ' Get a reference to the selected category pcCat = CType(cboCategories.SelectedItem, PerformanceCounterCategory) ' Fill cboCounters with the list of counters. cboCounters.Items.Clear() ' Get a collection of counters for this category Dim idcc As InstanceDataCollectionCollection = pcCat.ReadCategory ' Each element of this collection is associated with a counter. Dim cntName As String For Each cntName In idcc.Keys cboCounters.Items.Add(cntName) Next ' Fill cboInstances with the list of instances. cboInstances.Items.Clear() cboInstances.Items.Add("(all)") Try For Each instName In pcCat.GetInstanceNames cboInstances.Items.Add(instName) Next Catch ex As Exception ' nothing to do - ignore the error End Try ' select the first element in both comboboxes If cboCounters.Items.Count > 0 Then cboCounters.SelectedIndex = 0 End If If cboInstances.Items.Count > 0 Then cboInstances.SelectedIndex = 0 End If End Sub ' refresh display when a new counter/instance is selected Private Sub CounterInstanceChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCounters.SelectedIndexChanged, cboInstances.SelectedIndexChanged ' Create the array of 1 or more PerformanceCounter objects Dim catName As String = CType(cboCategories.SelectedItem, PerformanceCounterCategory).CategoryName Dim cntName As String = cboCounters.SelectedItem.ToString ' Two cases If cboInstances.SelectedIndex > 0 Then ' One single instance Dim instName As String = cboInstances.SelectedItem.ToString ReDim counters(0) counters(0) = New PerformanceCounter(catName, cntName, instName) Else ' Multiple instances Dim pcCat As PerformanceCounterCategory pcCat = CType(cboCategories.SelectedItem, PerformanceCounterCategory) Dim instNames() As String = pcCat.GetInstanceNames ReDim counters(instNames.GetUpperBound(0)) Dim i As Integer For i = 0 To UBound(counters) counters(i) = New PerformanceCounter(catName, cntName, instNames(i)) Next End If RefreshValues() End Sub ' The list of counters to be refreshed Dim counters() As PerformanceCounter Dim samples() As CounterSample ' update the display of selected counters/instances Private Sub RefreshValues() Dim sb As New System.Text.StringBuilder(1000) Dim pCnt As PerformanceCounter ' Get the list of values For Each pCnt In counters sb.Append(pCnt.InstanceName) sb.Append(" = ") sb.Append(GetCounterValue(pCnt)) sb.Append(ControlChars.CrLf) Next ' Display in textbox txtOut.Text = sb.ToString End Sub ' Get the value of a given PerformanceCounter object Private Function GetCounterValue(ByVal pCnt As PerformanceCounter) As String Select Case pCnt.CounterType Case PerformanceCounterType.NumberOfItems32, PerformanceCounterType.NumberOfItems64 Return pCnt.RawValue.ToString Case PerformanceCounterType.RateOfCountsPerSecond32, PerformanceCounterType.RateOfCountsPerSecond64 Return pCnt.NextValue.ToString Case PerformanceCounterType.AverageTimer32 Return pCnt.NextValue.ToString End Select End Function ' create a new performance counter Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click Dim counters As New CounterCreationDataCollection() Dim ccd As CounterCreationData ' Exit if the category exists already. If PerformanceCounterCategory.Exists("MyApp") Then Exit Sub End If ' Define the first counter, then add it to the collection. ccd = New CounterCreationData("Flush operations", "Total number of flush operations", PerformanceCounterType.NumberOfItems32) counters.Add(ccd) ' Define the second counter, then add it to the collection. ccd = New CounterCreationData("Flush operations / sec", "Number of flush operations per second", PerformanceCounterType.RateOfCountsPerSecond32) counters.Add(ccd) ' Create the category and the counters in one operation. PerformanceCounterCategory.Create("MyApp", "Counters for MyApp program", counters) End Sub ' generate some custom values Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim pc1 As New PerformanceCounter("MyApp", "Flush operations", False) Dim pc2 As New PerformanceCounter("MyApp", "Flush operations / sec", False) Dim res As Single txtOut.Text = "" ' Assign a starting value. pc1.RawValue = 10 pc2.RawValue = 10 ' store an initial sample. Dim cs1 As CounterSample = pc2.NextSample ' Wait some time and increment values. System.Threading.Thread.Sleep(500) pc1.IncrementBy(15) pc2.IncrementBy(15) ' Display current values res = CounterSample.Calculate(cs1, pc2.NextSample) LogMessage("Flush operations = " & pc1.RawValue.ToString) LogMessage("Flush operations / sec = " & res.ToString) ' Wait some time and increment values. System.Threading.Thread.Sleep(500) pc1.IncrementBy(25) pc2.IncrementBy(25) ' Display current values res = CounterSample.Calculate(cs1, pc2.NextSample) LogMessage("Flush operations = " & pc1.RawValue.ToString) LogMessage("Flush operations / sec = " & res.ToString) Dim pc3 As New PerformanceCounter("MyApp", "Flush operations", False) ' Create a first instance. pc3.InstanceName = "First" pc3.RawValue = 10 ' Create a second instance. pc3.InstanceName = "Second" pc3.RawValue = 20 ' Display the value of the two instances. pc3.InstanceName = "First" Debug.WriteLine("First Instance = " & pc3.RawValue.ToString) pc3.InstanceName = "Second" Debug.WriteLine("Second Instance = " & pc3.RawValue.ToString) End Sub ' Destroy the custom category built previously. Private Sub btnDestroy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDestroy.Click ' check whether the category exists. If PerformanceCounterCategory.Exists("MyApp") Then PerformanceCounterCategory.Delete("MyApp") End If End Sub End Class